import time

from hypium import BY
from hypium.model import MatchPattern

from devicetest.core.test_case import TestCase, Step, CheckPoint
from hypium import *
# 类名 要跟文件名保持一致！
class JiaoYinShiLuoDe_003(TestCase):
    def __init__(self, controllers):
        self.TAG = self.__class__.__name__
        TestCase.__init__(self, self.TAG, controllers)
        self.driver = UiDriver(self.device1)

    def setup(self):
        Step('1.回到桌面')
        self.driver.swipe_to_home()
        Step('清除数据')
        self.driver.clear_app_data(package_name="com.fund001.etradingapp.hos")

    # 打开“交银基金”App
    def process(self):
        Step('打开“交银基金”APP')
        self.driver.start_app(package_name="com.fund001.etradingapp.hos")

        # ========== 新增 CheckPoint 验证：隐私政策弹窗 ==========
        CheckPoint('在启动页上显示同意隐私政策弹窗，包含【同意并使用】按钮和【仅浏览】按钮')
        # 验证“同意并使用”按钮存在
        agree_btn = self.driver.find_component(BY.text("同意并使用"))
        # 验证“仅浏览”按钮存在
        browse_btn = self.driver.find_component(BY.text("仅浏览"))
        assert agree_btn is not None, "未找到“同意并使用”按钮"
        assert browse_btn is not None, "未找到“仅浏览”按钮"

        Step('点击【仅浏览】按钮')
        x2 = self.driver.find_component(BY.text("仅浏览"))  # 找到 按钮
        x2 = self.driver.touch(BY.text("仅浏览"))  # 并点击

        # 这里需要等待5s才能进入到首页，因为有广告
        time.sleep(5)
        # ========== 新增 CheckPoint 验证：以游客模式进入App首页 ==========
        CheckPoint('进入App首页')
        home_page_btn = self.driver.find_component(BY.text("首页"))
        assert home_page_btn is not None, "未找到“首页”按钮"

    def teardown(self):
        Step('关闭交银基金APP')
        self.driver.stop_app("com.fund001.etradingapp.hos")